package tablet.module_user.fragment.studentcollect;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.json_deal.lib_mgson.MGson;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpDialogCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.StudentCollectedFamousResBean;
import tablet.module_user.bean.StudentCollectedVideoBean;

public class StudentFamousResFragment extends BaseAppFragment {

    @BindView(R2.id.rv_video)
    RecyclerView rvVideo;
    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    int page = 0;
    HomeAdapter homeAdapter;
    List<StudentCollectedFamousResBean.ObjBean.RowsBean> list = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initStudentCollectResData();
            }
        });

        initView();

        initStudentCollectResData();

    }

    private void initStudentCollectResData() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "STUDENT_FAVORITE_LIST")
                .addParam("token", SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken())
                .addParam("page", page)
                .addParam("limit", 10)
                .addParam("lbk_or_jfzl", 2)
                .execute(new HttpDialogCallBack<StudentCollectedFamousResBean>() {
                    @Override
                    public void onSuccess(StudentCollectedFamousResBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {
                            if (result.getObj() != null) {
                                if (page == 0 && list.size() > 0) {
                                    list.clear();
                                }
                                list.addAll(result.getObj().getRows());
                                homeAdapter.notifyDataSetChanged();
                            }

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {

                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }

                    @Override
                    public void onError(String e) {
                        stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                });


    }

    private void initView() {

        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                initStudentCollectResData();

                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++page;
                initStudentCollectResData();
                springView.onFinishFreshAndLoad();
            }
        });

        homeAdapter = new HomeAdapter(getActivity(), list);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(getActivity());
        rvVideo.setLayoutManager(manager);
        rvVideo.setAdapter(homeAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_userecord;
    }


    private class HomeAdapter extends BaseAdapter<StudentCollectedFamousResBean.ObjBean.RowsBean> {

        public HomeAdapter(Context context, List<StudentCollectedFamousResBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_study_famousres;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<StudentCollectedFamousResBean.ObjBean.RowsBean> mData) {

            final StudentCollectedFamousResBean.ObjBean.RowsBean rowsBean = mData.get(position);

            if (StringUtil.isNotEmpty(rowsBean.getTitle(), true)) {

                holder.setText(R.id.tv_title, rowsBean.getTitle());
            }

            holder.setOnClickListener(R.id.tv_see, new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                }
            });

        }

    }
}
